package com.example.myapplication.network;

import com.example.myapplication.models.Banner;
import com.example.myapplication.models.Category;
import com.example.myapplication.models.Course;

import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {

    private static RetrofitClient instance;
    private ApiService apiService;
    private MockApiService mockApiService;

    private RetrofitClient() {
        // 初始化Retrofit
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://your-real-api.com/") // 替换为你的真实API基础URL
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        apiService = retrofit.create(ApiService.class);
        mockApiService = new MockApiService();
    }

    public static RetrofitClient getInstance() {
        if (instance == null) {
            instance = new RetrofitClient();
        }
        return instance;
    }

    // 获取轮播图数据
    public void getBanners(final ApiCallback<List<Banner>> callback) {
        // 这里为了演示，我们使用模拟数据
        // 你可以根据实际情况选择使用真实API或模拟数据
        callback.onSuccess(mockApiService.getBanners());

        // 真实API调用的示例（注释掉）：
        /*
        Call<List<Banner>> call = apiService.getBanners();
        call.enqueue(new Callback<List<Banner>>() {
            @Override
            public void onResponse(Call<List<Banner>> call, Response<List<Banner>> response) {
                if (response.isSuccessful()) {
                    callback.onSuccess(response.body());
                } else {
                    callback.onError("Failed to load banners");
                }
            }

            @Override
            public void onFailure(Call<List<Banner>> call, Throwable t) {
                callback.onError(t.getMessage());
            }
        });
        */
    }

    // 获取分类数据
    public void getCategories(final ApiCallback<List<Category>> callback) {
        callback.onSuccess(mockApiService.getCategories());
    }

    // 获取推荐课程
    public void getRecommendedCourses(final ApiCallback<List<Course>> callback) {
        callback.onSuccess(mockApiService.getRecommendedCourses());
    }

    // 获取课程详情
    public void getCourseDetail(int courseId, final ApiCallback<Course> callback) {
        callback.onSuccess(mockApiService.getCourseDetail(courseId));
    }

    // 获取分类下的课程列表
    public void getCoursesByCategory(int categoryId, final ApiCallback<List<Course>> callback) {
        callback.onSuccess(mockApiService.getCoursesByCategory(categoryId));
    }

    public interface ApiCallback<T> {
        void onSuccess(T data);
        void onError(String message);
    }
}